<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.List, com.petcommunity.model.Owner" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    List<Owner> owners = (List<Owner>) request.getAttribute("owners");
%>
<%
    String role = (String) session.getAttribute("role");
%>
<html>
<head>
    <title>宠物主人管理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://at.alicdn.com/t/c/font_4211259_0u5hm8ka6caa.css">
    <link rel="stylesheet" href="css/management.css">
</head>
<body>

    <div class="page-container">
        <!-- 装饰性爪印 -->
        <div class="decorative-paws paw-1">🐾</div>
        <div class="decorative-paws paw-2">🐾</div>
        <div class="decorative-paws paw-3">🐾</div>
        <div class="decorative-paws paw-4">🐾</div>

        <div class="main-container">
            <!-- 页面头部 -->
            <div class="page-header">
                <div class="header-content">
                    <div class="header-icon">👥</div>
                    <h2 class="page-title">宠物主人管理</h2>
                    <p class="page-subtitle">用心维系每一份信任</p>
                </div>
            </div>

            <!-- 主要内容区域 -->
            <div class="content-wrapper">
                <c:if test="${role == 'admin'}">
                    <!-- 添加主人表单 -->
                    <div class="form-section">
                        <div class="section-header">
                            <h3 class="section-title">
                                <span class="section-icon">➕</span>
                                添加新主人
                            </h3>
                        </div>
                        <form method="post" action="owners" class="add-form">
                            <div class="form-row">
                                <div class="form-group">
                                    <label for="name">
                                        <i class="icon-user"></i>
                                        姓名
                                    </label>
                                    <input type="text" id="name" name="name" placeholder="请输入主人姓名" required>
                                </div>
                                <div class="form-group">
                                    <label for="phone">
                                        <i class="icon-phone"></i>
                                        电话
                                    </label>
                                    <input type="text" id="phone" name="phone" placeholder="请输入联系电话" required>
                                </div>
                                <div class="form-group">
                                    <label for="address">
                                        <i class="icon-location"></i>
                                        地址
                                    </label>
                                    <input type="text" id="address" name="address" placeholder="请输入详细地址" required>
                                </div>
                            </div>
                            <div class="form-actions">
                                <button type="submit" class="submit-btn">
                                    <span class="btn-icon">✨</span>
                                    添加主人
                                </button>
                            </div>
                        </form>
                    </div>
                </c:if>

                <!-- 主人列表 -->
                <div class="table-section">
                    <div class="section-header">
                        <h3 class="section-title">
                            <span class="section-icon">📋</span>
                            主人列表
                        </h3>
                        <div class="table-info">
                            <span class="record-count">共 ${owners.size()} 条记录</span>
                        </div>
                    </div>

                    <div class="table-container">
                        <table class="data-table">
                            <thead>
                                <tr>
                                    <th>
                                        <span class="th-content">
                                            <i class="icon-id"></i>
                                            ID
                                        </span>
                                    </th>
                                    <th>
                                        <span class="th-content">
                                            <i class="icon-user"></i>
                                            姓名
                                        </span>
                                    </th>
                                    <th>
                                        <span class="th-content">
                                            <i class="icon-phone"></i>
                                            电话
                                        </span>
                                    </th>
                                    <th>
                                        <span class="th-content">
                                            <i class="icon-location"></i>
                                            地址
                                        </span>
                                    </th>
                                    <c:if test="${role == 'admin'}">
                                    <th>
                                        <div class="th-content">
                                            <i class="fas fa-cogs"></i>
                                            操作
                                        </div>
                                    </th>
                                    </c:if>
                                </tr>
                            </thead>
                            <tbody>
                                <c:forEach var="o" items="${owners}" varStatus="status">
                                    <tr class="table-row" data-index="${status.index}">
                                        <td class="id-cell">${o.ownerId}</td>
                                        <td class="name-cell">
                                            <div class="user-info">
                                                <div class="user-avatar">${o.name.substring(0,1)}</div>
                                                <span class="user-name">${o.name}</span>
                                            </div>
                                        </td>
                                        <td class="phone-cell">
                                            <a href="tel:${o.phone}" class="phone-link">${o.phone}</a>
                                        </td>
                                        <td class="address-cell">
                                            <span class="address-text">${o.address}</span>
                                        </td>
                                        <c:if test="${role == 'admin'}">
                                        <td data-label="操作">
                                            <div class="action-buttons">
                                                <button class="edit-btn" onclick="editowners(${o.ownerId})">
                                                    <i class="fas fa-edit"></i>编辑
                                                </button>
                                                <button class="delete-btn" onclick="deleteowners(${o.ownerId})">
                                                    <i class="fas fa-trash"></i>删除
                                                </button>
                                            </div>
                                        </td>
                                        </c:if>
                                    </tr>
                                </c:forEach>
                            </tbody>
                        </table>

                        <c:if test="${empty owners}">
                            <div class="empty-state">
                                <div class="empty-icon">📝</div>
                                <h3>暂无数据</h3>
                                <p>还没有添加任何宠物主人信息</p>
                            </div>
                        </c:if>
                    </div>
                </div>

                <!-- 返回按钮 -->
                <div class="back-section">
                    <a href="dashboard.jsp" class="back-btn">
                        <span class="btn-icon">🏠</span>
                        返回首页
                    </a>
                </div>
            </div>
        </div>
    </div>

    <script>
// 编辑通知
function editowners(id) {
    if (confirm('确定要编辑这名主人的信息吗？')) {
        window.location.href = 'edit?type=owner&id=' + id;
    }
}

// 删除通知
function deleteowners(id) {
    if (confirm('确定要删除这名主人吗？此操作不可恢复！')) {
        window.location.href = 'delete?type=owner&id=' + id;
    }
}

</script>

</body>
</html>